package com.wish.group05.file.controller;

import com.qiniu.util.Auth;
import com.wish.group05.file.service.QiniuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 作者:李皞
 * 日期:2022/5/24
 **/

@RestController
@RequestMapping("/qiniu")
@CrossOrigin
public class QiniuController {




    @Autowired
    private QiniuService qiniuService;

    @PostMapping("upload")
    public String uploadImage(MultipartFile file, HttpServletRequest request) {

        if(file.isEmpty()) {
            return "error";
        }

        try {
            String fileUrl=qiniuService.saveImage(file);
            return fileUrl;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }

    // 设置好账号的ACCESS_KEY和SECRET_KEY
    private String ACCESS_KEY = "YmE5ig0BEI8ZOUhLhLB3K0qaywQdg8x4NqHMjXZ3";
    private String SECRET_KEY = "VHieCFiTuXyk5lm3NJWLkhwQCFfrkPCH0H-x3EvK";
    // 要上传的空间
    private String bucketname = "java-group05";

    /**
     * 生成七牛云的token(上传凭证)
     * @return
     */
    @GetMapping("getToken")
    public String getToken(){
        System.err.println(111);
        //获取七牛云密钥
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        return auth.uploadToken(bucketname);
    }
}

